package com.rockwell.robot2015;

import org.opencv.core.Point;

import com.rockwell.robot2015.R;
import com.rockwell.robot2015.model.ColorBlobInfo;
import com.rockwell.robot2015.model.OnColorBlobDetectListener;
import com.rockwell.robot2015.model.OnOrientationChangeListener;
import com.rockwell.robot2015.model.Robot;
import com.rockwell.robot2015.model.command.RobotCommander;
import com.rockwell.robot2015.model.detectors.DropAreaDetector;
import com.rockwell.robot2015.model.detectors.IDetector;
import com.rockwell.robot2015.model.detectors.Maze1Detector;
import com.rockwell.robot2015.model.detectors.Maze2Detector;
import com.rockwell.robot2015.model.detectors.Maze3Detector;
import com.rockwell.robot2015.model.detectors.MazeEntranceGuideDetector;
//import com.rockwell.robot2015.model.detectors.ObstacleDetector;
import com.rockwell.robot2015.model.detectors.OuterWallDetector;
import com.rockwell.robot2015.model.detectors.TreasureDetector;
import com.rockwell.robot2015.model.sensors.RobotSensors;
import com.rockwell.robot2015.model.solvers.LeftFirstMazeSolver;
import com.rockwell.robot2015.views.MazeCameraView;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TeachActivity extends Activity {
	private MazeCameraView	m_mazeCameraView;

	private IDetector m_treasureDetector;
	private IDetector m_obstacleDetector;
	private IDetector m_maze1Detector;
	private IDetector m_maze2Detector;
	private IDetector m_maze3Detector;
	private IDetector m_mazeEntranceGuideDetector;
	private IDetector m_outerWallDetector;
	private IDetector m_dropAreaDetector;

	private OnColorBlobDetectListener m_treasureListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(boolean present, ColorBlobInfo info, int length,
				int width) {
			m_treasureDetector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_obstacleListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			m_obstacleDetector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_mazeEntranceGuideListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			m_mazeEntranceGuideDetector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_dropAreaListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			m_dropAreaDetector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_outerWallListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			m_outerWallDetector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_maze1Listener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			m_maze1Detector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_maze2Listener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			m_maze2Detector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_maze3Listener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			m_maze3Detector.onDetect(present, info, length, width);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_teach);
		
		m_mazeCameraView = (MazeCameraView) this.findViewById(R.id.maze_camera_view);
		m_mazeCameraView.setTeachMode(true);

		//m_mazeCameraView.setOrientationChangeListener(m_orientationListener);
		m_mazeCameraView.setTreasureListener(m_treasureListener);
		//m_mazeCameraView.setObstacleListener(m_obstacleListener);
		m_mazeCameraView.setMaze1Listener(m_maze1Listener);
		m_mazeCameraView.setMaze2Listener(m_maze2Listener);
		m_mazeCameraView.setMaze3Listener(m_maze3Listener);
		m_mazeCameraView
				.setMazeEntranceGuideListener(m_mazeEntranceGuideListener);
		m_mazeCameraView.setOuterWallListener(m_outerWallListener);
		m_mazeCameraView.setDropAreaListener(m_dropAreaListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.robot, menu);
		return true;
	}

	@Override
	public void onStart() {
		super.onStart();

		m_treasureDetector = new TreasureDetector(m_treasureListener);
		//m_obstacleDetector = new ObstacleDetector(m_obstacleListener);
		m_maze1Detector = new Maze1Detector(m_maze1Listener);
		m_maze2Detector = new Maze2Detector(m_maze2Listener);
		m_maze3Detector = new Maze3Detector(m_maze3Listener);
		m_mazeEntranceGuideDetector = new MazeEntranceGuideDetector(m_mazeEntranceGuideListener);
		m_outerWallDetector = new OuterWallDetector(m_outerWallListener);
		m_dropAreaDetector = new DropAreaDetector(m_dropAreaListener);
	}
	
	@Override
    public void onPause()
    {
        super.onPause();
        m_mazeCameraView.onDeactivated();
    }

	@Override
	public void onStop() {

		super.onStop();
		m_mazeCameraView.onDeactivated();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		m_mazeCameraView.onActivated();
	}
}
